package view.mediators
{
	import controller.MoveEvent;
	
	import flash.events.*;
	import flash.net.SharedObject;
	import flash.utils.*;
	
	import model.BoardProxy;
	import model.Piece;
	import model.attack.Defuse;
	import model.attack.Force;
	import model.attack.NormalAttack;
	import model.moving.Charging;
	import model.moving.NoMove;
	import model.moving.NormalMove;
	import model.moving.Quickness;
	import model.moving.Slashing;
	
	import mx.controls.*;
	import mx.core.*;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.stBoard;
	import view.stSquare;
  
  	/** The Board class holds references to all the squares*/
	public class BoardMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "BoardMediator";
		public static const PIECE_MOVED:String = "Piece Moved";
		
		private var mxmlApp:Application; //The stratego application
		private var playBoard:Object; //The board display object
		private var board:Array; // The 10x10 array of squares
		private var emptyPiece:Piece = new Piece('Empty',0,'images/empty.jpg', new NoMove()); // a blank spaceholder
		
		public var so:SharedObject; // a local shared object
		public var lightPiece:Array;
		public var darkPiece:Array;
		
		public var tempX1:Number = -1;
		public var tempY1:Number = -1;
		
		/** Constructor. sets up the board, and shared object. */
		public function BoardMediator(view:stBoard){
			super(NAME, view);
			board = new Array();
			//mxmlApp = Application(Application.application);
			//playBoard = mxmlApp.getChildByName('playBoard');
			playBoard = view;
			so = SharedObject.getLocal('so');
			so.clear();
			so.data.empty = this.emptyPiece;
			this.main();
			viewBoard.addEventListener(stBoard.TEST, startTest);
		}
		
		protected function get viewBoard():stBoard{
			return viewComponent as stBoard;
		}
		
		private function startTest(e:Event):void{
			setupDarkPieces();
			setupDark();
			setupLightPieces();
			setupLight();
		}
		
		public function pieceMoved(x1:Number, y1:Number, x2:Number, y2:Number):void{
			proxy.sendMove(x1,y1,x2,y2);
		}
		
		private function get proxy():BoardProxy{
			return facade.retrieveProxy(BoardProxy.NAME) as BoardProxy;
		}
		
		override public function listNotificationInterests():Array{
			return [
					BoardMediator.PIECE_MOVED
					];
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case BoardMediator.PIECE_MOVED:
					var moveEvent:MoveEvent = notification.getBody() as MoveEvent;
					this.board[moveEvent.x1][moveEvent.y1].removePiece();
					this.board[moveEvent.x2][moveEvent.y2].receivedMove();
					break;
			}
		}
		
		/**
		 * The main function initializes all the squares
		 */
		public function main():void{
			
			var i:Number;
			var j:Number;
			
			for (i=0; i< 10; i++){
				board[i] = new Array();
				for (j=0; j < 10; j++){
					board[i][j] = new stSquare();
					playBoard.addChild(board[i][j]);
					board[i][j].x = i*40 + 1;
					board[i][j].y = j*40 + 1;
					board[i][j].setBoard(this, i, j);
				}
			}
			asteroids();
			
		}
		
		/**
		 * Initializes all the asteroid squares.
		 */
		public function asteroids():void{
			board[2][4].setPassable(false);
			board[3][4].setPassable(false);
			board[2][5].setPassable(false);
			board[3][5].setPassable(false);
			
			board[6][4].setPassable(false);
			board[7][4].setPassable(false);
			board[6][5].setPassable(false);
			board[7][5].setPassable(false);
		}
		
		/**
		 * Sets up some tests
		 */
		public function testPiece():void{
			board[1][1].addPiece(new Piece('Yoda',10, 'images/yoda.jpg', new NormalMove()));
			board[0][0].addPiece(new Piece('bomb', 11, 'images/bomb.jpg', new NoMove()));
			board[3][1].addPiece(new Piece('droid', 2, 'images/bdroid.jpg', new NormalMove()));
			board[3][2].addPiece(new Piece('Emperor',10,'images/sidius.jpg', new NormalMove()));
			board[4][1].addPiece(new Piece('Anakin',7,'images/anakin.jpg', new Slashing()));
			board[5][1].addPiece(new Piece('Chewy',7,'images/chewy.jpg', new Quickness()));
			board[6][2].addPiece(new Piece('Dooku',8,'images/dooku.jpg', new Charging()));
			board[6][1].addPiece(new Piece('Defuser',3,'images/r2.jpg',new NormalMove(), new Defuse()));
			board[6][0].addPiece(new Piece('Light Force',1,'images/lside.jpg',new NormalMove(), new Force()));
		}
		
		public function setupLightPieces():void{
			lightPiece = new Array()
			var i:Number;
			
			for (i=0; i<=3; i++){
				lightPiece[i] = new Piece('Scout', 2, 'images/scout.jpg', new Charging(), new NormalAttack());
			}
			
			for (i=4; i<=7; i++){
				lightPiece[i] = new Piece('Clone', 2, 'images/clone.jpg', new Charging(), new NormalAttack());
			}
			
			lightPiece[8] = new Piece('Defuser', 3, 'images/r2.jpg', new NormalMove(), new Defuse());
			lightPiece[9] = new Piece('Defuser', 3, 'images/r4.jpg', new NormalMove(), new Defuse());
			lightPiece[10] = new Piece('Defuser', 3, 'images/r5.jpg', new NormalMove(), new Defuse());
			lightPiece[11] = new Piece('Defuser', 3, 'images/ru-1.jpg', new NormalMove(), new Defuse());
			lightPiece[12] = new Piece('Defuser', 3, 'images/ru-2.jpg', new NormalMove(), new Defuse());
			
			lightPiece[13] = new Piece('RedWing',4,'images/helmet.jpg', new NormalMove(), new NormalAttack());
			lightPiece[14] = new Piece('RedWing',4,'images/helmet.jpg', new NormalMove(), new NormalAttack());
			lightPiece[15] = new Piece('Wedge',4,'images/wedge.jpg', new NormalMove(), new NormalAttack());
			lightPiece[16] = new Piece('Wedge',4,'images/wedge.jpg', new NormalMove(), new NormalAttack());
			
			lightPiece[17] = new Piece('Leia', 5, 'images/leia.jpg', new NormalMove(), new NormalAttack());
			lightPiece[18] = new Piece('Organa', 5, 'images/organa.jpg', new NormalMove(), new NormalAttack());
			
			lightPiece[19] = new Piece('Fitso', 6, 'images/fitso.jpg');
			lightPiece[20] = new Piece('Koon', 6, 'images/koon.jpg');
			lightPiece[21] = new Piece('Mundi', 6, 'images/mundi.jpg');
			lightPiece[22] = new Piece('Qui Gon', 6, 'images/quigon.jpg');
			
			lightPiece[23] = new Piece('Anakin', 7, 'images/anakin.jpg', new Slashing(), new NormalAttack());
			lightPiece[24] = new Piece('Chewy',7,'images/chewy.jpg',new Quickness(), new NormalAttack());
			lightPiece[25] = new Piece('Han Solo',7,'images/solo.jpg', new Charging(), new NormalAttack());
			
			lightPiece[26] = new Piece('Obi Wan',8,'images/obiwan.jpg', new Quickness(), new NormalAttack());
			lightPiece[27] = new Piece('Windu',8,'images/windu.jpg', new Charging(), new NormalAttack());
			
			lightPiece[28] = new Piece('Luke', 9, 'images/luke.jpg', new NormalMove(), new NormalAttack());
			lightPiece[29] = new Piece('Yoda', 10, 'images/yoda.jpg', new NormalMove(), new NormalAttack());
			
			lightPiece[30] = new Piece('Light Sabre', 1, 'images/lsaber.jpg', new NoMove(), new NormalAttack());
			
			lightPiece[31] = new Piece('Light Force', 1, 'images/lside.jpg', new NormalMove(), new Force());
			lightPiece[32] = new Piece('Lando', 5, 'images/lando.jpg', new NormalMove(), new NormalAttack());
			
			lightPiece[33] = new Piece('Barbarian', 1, 'images/lbar.jpg', new NormalMove(), new NormalAttack());
			
			for (i = 34; i<=39; i++){
				lightPiece[i] = new Piece('Thermal Detonator', 11, 'images/bomb.jpg', new NormalMove(), new NormalAttack());
			}
		}
		
		public function setupDarkPieces():void{
			darkPiece = new Array();
			var i:Number;
			
			for (i=0; i<=3; i++){
				darkPiece[i] = new Piece('Battle Droid', 2, 'images/bdroid.jpg', new Charging(), new NormalAttack());
			}
			
			for (i=4; i<=7; i++){
				darkPiece[i] = new Piece('Strom Trooper', 2, 'images/storm.jpg', new Charging(), new NormalAttack());
			}
			
			darkPiece[8] = new Piece('Defuser', 3, 'images/sdroid.jpg', new NormalMove(), new Defuse());
			darkPiece[9] = new Piece('Defuser', 3, 'images/sdroid.jpg', new NormalMove(), new Defuse());
			darkPiece[10] = new Piece('Defuser', 3, 'images/sdroid.jpg', new NormalMove(), new Defuse());
			darkPiece[11] = new Piece('Defuser', 3, 'images/sdroid.jpg', new NormalMove(), new Defuse());
			darkPiece[12] = new Piece('Defuser', 3, 'images/sdroid.jpg', new NormalMove(), new Defuse());
			
			darkPiece[13] = new Piece('Destroyer',4,'images/droideka.jpg', new NormalMove(), new NormalAttack());
			darkPiece[14] = new Piece('Guard',4,'images/guard.jpg', new NormalMove(), new NormalAttack());
			darkPiece[15] = new Piece('Destroyer',4,'images/droideka.jpg', new NormalMove(), new NormalAttack());
			darkPiece[16] = new Piece('Guard',4, 'images/guard.jpg', new NormalMove(), new NormalAttack());
			
			darkPiece[17] = new Piece('Jabba the Hutt', 5, 'images/jabba.jpg', new NormalMove(), new NormalAttack());
			darkPiece[18] = new Piece('Moff Tarkin', 5, 'images/tarkin.jpg', new NormalMove(), new NormalAttack());
			
			darkPiece[19] = new Piece('Battle Droid', 6, 'images/sbdroid.jpg');
			darkPiece[20] = new Piece('Battle Droid', 6, 'images/sbdroid.jpg');
			darkPiece[21] = new Piece('Battle Droid', 6, 'images/sbdroid.jpg');
			darkPiece[22] = new Piece('Jango Fett', 6, 'images/jango.jpg');
			
			darkPiece[23] = new Piece('Grievous', 7, 'images/grievous.jpg', new Slashing(), new NormalAttack());
			darkPiece[24] = new Piece('Greedo',7,'images/greedo.jpg',new Quickness(), new NormalAttack());
			darkPiece[25] = new Piece('Boba Fett',7,'images/boba.jpg', new Charging(), new NormalAttack());
			
			darkPiece[26] = new Piece('Darth Maul',8,'images/maul.jpg', new Quickness(), new NormalAttack());
			darkPiece[27] = new Piece('Dooku',8,'images/dooku.jpg', new Charging(), new NormalAttack());
			
			darkPiece[28] = new Piece('Vader', 9, 'images/vader.jpg', new NormalMove(), new NormalAttack());
			darkPiece[29] = new Piece('Sidius', 10, 'images/sidius.jpg', new NormalMove(), new NormalAttack());
			
			darkPiece[30] = new Piece('Dark Sabre', 1, 'images/dsaber.jpg', new NoMove(), new NormalAttack());
			
			darkPiece[31] = new Piece('Dark Force', 1, 'images/dside.jpg', new NormalMove(), new Force());
			darkPiece[32] = new Piece('Assasin', 5, 'images/wessel.jpg', new NormalMove(), new NormalAttack());
			
			darkPiece[33] = new Piece('Barbarian', 1, 'images/dbar.jpg', new NormalMove(), new NormalAttack());
			
			for (i = 34; i<=39; i++){
				darkPiece[i] = new Piece('Thermal Detonator', 11, 'images/bomb.jpg', new NormalMove(), new NormalAttack());
			}
		}
		
		public function setupDark():void{
			var i:Number;
			for (i=0; i<= 39; i++){
				var newPiece:Piece = darkPiece[i];
				placePiece(newPiece, 0);
			}
		}
		
		public function setupLight():void{
			var i:Number;
			for (i=0; i<= 39; i++){
				var newPiece:Piece = lightPiece[i];
				placePiece(newPiece, 60);
			}
		}
		
		public function placePiece(piece:Piece, j:Number):void{
			var num:Number = Math.floor(Math.random()*40 + j);
			var x:Number = num % 10;
			var y:Number = Math.floor(num / 10);
			if (! board[x][y].isOccupied()){
				board[x][y].addPiece(piece);
			}
			else{
				placePiece(piece, j);
			}
		}
		
	}
}